package com.frame.service.impl;

import com.frame.domain.AgrGrowRecords;
import com.frame.domain.Crop;
import com.frame.domain.CropPlantiotin;
import com.frame.mapper.CropMapper;
import com.frame.mapper.PlantingMethodService;
import com.frame.service.CropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CropServiceImpl implements CropService {

    @Autowired
    private CropMapper cropMapper;

    @Override
    public void insertUsers(Crop crop) {
        cropMapper.insertUsers(crop);
    }


    @Autowired
    private PlantingMethodService plantingMethodService;

    @Override
    public List<Crop> list(Crop crop) {
        return cropMapper.list(crop);
    }

    @Override
    public boolean add(Crop crop) {

        boolean b=cropMapper.add(crop);
        if(b)
        {
            ArrayList<CropPlantiotin> arrayList = new ArrayList<>();


            Integer[] plantationIds = crop.getPlantationIds();
            for (Integer plantationId : plantationIds) {
                CropPlantiotin cropPlantiotin = new CropPlantiotin();
                cropPlantiotin.setCropId(crop.getCropId());
                cropPlantiotin.setPlantitionId(plantationId);
                cropPlantiotin.setCreateTime(new Date());
                arrayList.add(cropPlantiotin);
            }

            boolean b1=plantingMethodService.addmapper(arrayList);
            if(b1)
            {
                return true;
            }else
            {
                return false;
            }
        }else
        {
            return false;
        }
    }

    @Override
    public Crop hx(Integer cropId) {
        return cropMapper.hx(cropId);
    }

    @Override
    public boolean updata(Crop crop) {
        return cropMapper.updata(crop);
    }

    @Override
    public List<Crop> list1() {
        return cropMapper.list1();
    }

    @Override
    public List<Crop> getProcessList() {
        return cropMapper.getProcessList();
    }


    @Override
    public List<Crop> cropList() {
        return cropMapper.cropList();
    }

    @Override
    public boolean add2(AgrGrowRecords agrGrowRecords) {
        return cropMapper.add2(agrGrowRecords);
    }

    @Override
    public void updatastatu(Integer cropId) {
        cropMapper.updatastatu(cropId);
    }
}
